package com.ddstation.onsitelaw.service;


import com.ddstation.common.util.IdGenerateUtil;
import com.ddstation.onsitelaw.entity.MaInvsIcd10DiseaseEntity;
import com.ddstation.onsitelaw.entity.auditentity.DtHealthDataOutpatientMedicalEntity;
import com.ddstation.onsitelaw.enumerate.GenderEnum;
import com.ddstation.onsitelaw.enumerate.YbTypeEnum;
import com.ddstation.onsitelaw.mapper.DtHealthDataOutpatientMedicalMapper;
import com.ddstation.onsitelaw.mapper.MaInvsIcd10DiseaseMapper;
import com.ddstation.onsitelaw.model.OutpatientMedicalModel;
import com.ddstation.onsitelaw.utils.StringHandleUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import javax.inject.Inject;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class OutpatientServiceImpl implements OutpatientService {
    @Inject
    private DtHealthDataOutpatientMedicalMapper dtHealthDataOutpatientMedicalMapper;
    @Inject
    private MaInvsIcd10DiseaseMapper maInvsIcd10DiseaseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public DtHealthDataOutpatientMedicalEntity addMedical(OutpatientMedicalModel medical) {

        DtHealthDataOutpatientMedicalEntity outpatientMedical = new DtHealthDataOutpatientMedicalEntity();
        //entid唯一码(对应病历表的outerMedicalId)
        outpatientMedical.setOuterMedicalId(medical.getOuterMedicalId());
        outpatientMedical.setGender(GenderEnum.getCode(medical.getGender()));
        outpatientMedical.setName(medical.getName());
        outpatientMedical.setAge(StringHandleUtil.getNumbers(medical.getAge()));
        outpatientMedical.setYbPersonalNo(medical.getYbPersonalNo());
        outpatientMedical.setYbType(YbTypeEnum.getCode(medical.getYbType()));
        outpatientMedical.setDiagnosis(medical.getDiagnosis());
        outpatientMedical.setDiagnosisIcd10(getDiagnosisIcd10(medical.getDiagnosis()));
        outpatientMedical.setDoctorId(medical.getDoctorId());
        outpatientMedical.setInstitutionId(medical.getInstitutionId());
        //outpatientMedical.setUId(medical.getPatId());
        outpatientMedical.setId(IdGenerateUtil.idGenerateFromTime("04"));
        outpatientMedical.setSourceType("04");
        outpatientMedical.setUpdateTm(new Date());
        Example example = new Example(DtHealthDataOutpatientMedicalEntity.class);
        //组装sql执行条件
        splitSql(outpatientMedical, example);
        //数据存在就更新，不存在就新增。
        List<DtHealthDataOutpatientMedicalEntity> restList = dtHealthDataOutpatientMedicalMapper.selectByExample(example);
        if (ObjectUtils.isEmpty(restList)) {
            dtHealthDataOutpatientMedicalMapper.insertSelective(outpatientMedical);
        } else {
            dtHealthDataOutpatientMedicalMapper.updateByExampleSelective(outpatientMedical, example);
        }
        return outpatientMedical;

    }


    /**
     * 组装sql执行条件
     *
     * @param outpatientMedical
     * @param example
     */
    private void splitSql(DtHealthDataOutpatientMedicalEntity outpatientMedical, Example example) {
        //组装sql执行条件
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("ybPersonalNo", outpatientMedical.getYbPersonalNo());
        criteria.andEqualTo("institutionId", outpatientMedical.getInstitutionId());
    }

    String getDiagnosisIcd10(String diagnosis) {
        //组装sql执行条件
        Example example = new Example(MaInvsIcd10DiseaseEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andLike("AKA121", diagnosis);
        List<MaInvsIcd10DiseaseEntity> list = maInvsIcd10DiseaseMapper.selectByExample(example);
        List<String> AKA120List = list.stream().map(i -> i.getAKA120()).collect(Collectors.toList());
        String AKA120Str = String.join(",", AKA120List);
        return AKA120Str;
    }
}